package bomb.menus;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import bomb.Game;
import bomb.utils.Arquivo;
import bomb.utils.Highscores;
import bomb.utils.Letras;


public class LevelSelect extends BasicGameState {
    
    int ident = 0;
    
    
    Image back = null;
    Image level1 = null;
    Image level2 = null;
    Image level3 = null;
    Image level4 = null;
    Image level5 = null;
    Image level6 = null;
  

    
    float level1_scale = 1;
    float level2_scale = 1;
    float level3_scale = 1;
    float level4_scale = 1;
    float level5_scale = 1;
    float level6_scale = 1;
    boolean insideLevel1 = false;
    boolean insideLevel2 = false;
    boolean insideLevel3 = false;
    boolean insideLevel4 = false;
    boolean insideLevel5 = false;
    boolean insideLevel6 = false;
    
    
    float scaleStep = 0.001f;
    
    
    public LevelSelect(int id) {
        this.ident = id;
        
    }
    
    @Override
    public int getID() {
        return ident;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        try {
            Arquivo.gravar();
        } catch (IOException ex) {
            Logger.getLogger(LevelSelect.class.getName()).log(Level.SEVERE, null, ex);
        }
        back = new Image("src/images/menufundo2.png");
        Image options = new Image("src/images/menulevel.png");

        level1 = options.getSubImage(0, 0, 133, 100);
        level2 = options.getSubImage(133, 0, 133, 100);
        level3 = options.getSubImage(267, 0, 133, 100);
        level4 = options.getSubImage(0, 100, 133, 100);
        level5 = options.getSubImage(133, 100, 133, 100);
        level6 = options.getSubImage(267, 100, 133, 100);

        
        
        
    }
    @SuppressWarnings("deprecation")
    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        back.draw(0,0);


        level1.draw(0,130,level1_scale);
        level2.draw(133,130,level2_scale);
        level3.draw(267,130,level3_scale);
        level4.draw(0,260,level4_scale);
        level5.draw(133,260,level5_scale);
        level6.draw(267,260,level6_scale);
        
       
        if(insideLevel1){
            Letras.getLetra2().drawString(265, 60,"Nível 1", Color.white);
            Letras.getLetra1().drawString(290, 90,"Melhor: "+Integer.toString(Highscores.getScore(0, 0)), Color.white);
        }
        if(insideLevel2){
            Letras.getLetra2().drawString(265, 60,"Nível 2", Color.white);
            Letras.getLetra1().drawString(290, 90,"Melhor: "+Integer.toString(Highscores.getScore(1, 0)), Color.white);
        }
        if(insideLevel3){
            Letras.getLetra2().drawString(265, 60,"Nível 3", Color.white);
            Letras.getLetra1().drawString(290, 90,"Melhor: "+Integer.toString(Highscores.getScore(2, 0)), Color.white);
        }
        if(insideLevel4){
            Letras.getLetra2().drawString(265, 60,"Nível 4", Color.white);
            Letras.getLetra1().drawString(290, 90,"Melhor: "+Integer.toString(Highscores.getScore(3, 0)), Color.white);
        }
        if(insideLevel5){
            Letras.getLetra2().drawString(265, 60,"Nível 5", Color.white);
            Letras.getLetra1().drawString(290, 90,"Melhor: "+Integer.toString(Highscores.getScore(4, 0)), Color.white);
        }
        if(insideLevel6){
            Letras.getLetra2().drawString(265, 60,"Nível 6", Color.white);
            Letras.getLetra1().drawString(290, 90,"Melhor: "+Integer.toString(Highscores.getScore(5, 0)), Color.white);
        }
    }
    
    
    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {
        
        Input input = gc.getInput();
        if(input.isKeyPressed(Input.KEY_ESCAPE)) {
            sbg.enterState(Game.MENUSTATE);
        }
        
        int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();
        insideLevel1 = false;
        insideLevel2 = false;
        insideLevel3 = false;
        insideLevel4 = false;
        insideLevel5 = false;
        insideLevel6 = false;
        
        //dentro do level 1
        if( (mouseX >= 0 && mouseX < 133) && (mouseY >= 150 && mouseY < 250) ) {
            insideLevel1 = true;

        }
        //dentro do level 2
        if( (mouseX >= 133 && mouseX < 266) && (mouseY >= 150 && mouseY < 250) ) {
            insideLevel2 = true;
        }
        //dentro do level 3
        if( (mouseX >= 266 && mouseX < 400) && (mouseY >= 150 && mouseY < 250) ) {
            insideLevel3 = true;
        }
        //dentro do level 4
        if( (mouseX >= 0 && mouseX < 133) && (mouseY >= 280 && mouseY < 380)  ) {
            insideLevel4 = true;
        }
         //dentro do level 5
        if( (mouseX >= 133 && mouseX < 266) && (mouseY >= 280 && mouseY < 380 )) {
            insideLevel5 = true;
        }       
        //dentro do level 6
        if( (mouseX >= 266 && mouseX < 400) && (mouseY >= 280 && mouseY < 380 )) {
            insideLevel6 = true;
        }

        
        //--------------------------------------------------- ACOES
        
        //dentro do level 1
        if(insideLevel1) {
            if(level1_scale < 1.05f) {
                  level1_scale += scaleStep;
                  
            }
            if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                Game.setFaseAtual(0);
                Game.clickSound();
                input.clearKeyPressedRecord();
                sbg.enterState(Game.LEVEL1STATE);
            }
        }
        else {
            if(level1_scale > 1.0f) {
            level1_scale -= scaleStep;
            } 
        } 
        
        
        //dentro do level 2
        if(insideLevel2) {
              if(level2_scale < 1.05f) {
                  level2_scale += scaleStep;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  Game.setFaseAtual(1);
                  Game.clickSound();
                  input.clearKeyPressedRecord();
                  sbg.enterState(Game.LEVEL2STATE);
              }
        }
        else {
            if(level2_scale > 1.0f) {
            level2_scale -= scaleStep;
            } 
        }    
        
        //dentro do level 3
        if(insideLevel3) {
              if(level3_scale < 1.05f) {
                  level3_scale += scaleStep;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  Game.setFaseAtual(2);
                  Game.clickSound();
                  input.clearKeyPressedRecord();
                  sbg.enterState(Game.LEVEL3STATE);
              }
        }
        else {
            if(level3_scale > 1.0f) {
            level3_scale -= scaleStep;
            }
        }
        
        //dentro do level 4
        if(insideLevel4) {
              if(level4_scale < 1.05f) {
                  level4_scale += scaleStep;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  Game.setFaseAtual(3);
                  Game.clickSound();
                  input.clearKeyPressedRecord();
                  sbg.enterState(Game.LEVEL4STATE);
              }
        }
        else {
            if(level4_scale > 1.0f) {
            level4_scale -= scaleStep;
            } 
        } 
        
        //dentro do level 5
        if(insideLevel5) {
              if(level5_scale < 1.05f) {
                  level5_scale += scaleStep;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  Game.setFaseAtual(4);
                  Game.clickSound();
                  input.clearKeyPressedRecord();
                  sbg.enterState(Game.LEVEL5STATE);
              }
        }
        else {
            if(level5_scale > 1.0f) {
            level5_scale -= scaleStep;
            } 
        }
        
        //dentro do level 6
        if(insideLevel6) {
              if(level6_scale < 1.05f) {
                  level6_scale += scaleStep;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  Game.setFaseAtual(5);
                  Game.clickSound();
                  input.clearKeyPressedRecord();
                  sbg.enterState(Game.LEVEL6STATE);
              }
        }
        else {
            if(level6_scale > 1.0f) {
            level6_scale -= scaleStep;
            } 
        }
        
        
    }
    
}